package com.ruoyi.project.note.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.note.domain.Notebook;
import com.ruoyi.project.note.mapper.NotebookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.note.mapper.NoteMapper;
import com.ruoyi.project.note.domain.Note;
import com.ruoyi.project.note.service.INoteService;

/**
 * 笔记Service业务层处理
 * 
 * @author dou
 * @date 2024-03-07
 */
@Service
public class NoteServiceImpl implements INoteService 
{
    @Autowired
    private NoteMapper noteMapper;

    @Autowired
    private NotebookMapper notebookMapper;

    /**
     * 查询笔记
     * 
     * @param id 笔记主键
     * @return 笔记
     */
    @Override
    public Note selectNoteById(String id)
    {
        return noteMapper.selectNoteById(id);
    }

    /**
     * 查询笔记列表
     * 
     * @param note 笔记
     * @return 笔记
     */
    @Override
    public List<Note> selectNoteList(Note note)
    {
        return noteMapper.selectNoteList(note);
    }

    /**
     * 新增笔记
     * 
     * @param note 笔记
     * @return 结果
     */
    @Override
    public int insertNote(Note note)
    {
        Long notebookId = note.getNotebookId();
        if (notebookId!=null){
            Notebook notebook = notebookMapper.selectNotebookByNotebookId(notebookId);
            String notebookName = notebook.getNotebookName();
            note.setNotebookName(notebookName);
            int i=notebookMapper.updateNoteNum(null, notebookId);
        }
        return noteMapper.insertNote(note);
    }

    /**
     * 修改笔记
     * 
     * @param note 笔记
     * @return 结果
     */
    @Override
    public int updateNote(Note note)
    {
        Long id = note.getId();
        //获得修改前的笔记集id
        Note note_old = noteMapper.selectNoteById(String.valueOf(id));
        Long notebookId_old = note_old.getNotebookId();
        //获得修改后的笔记集id
        Long notebookId_new = note.getNotebookId();
        if (notebookId_new==null){
            note.setNotebookName("");
        }else {
            Notebook notebook = notebookMapper.selectNotebookByNotebookId(notebookId_new);
            String notebookName = notebook.getNotebookName();
            note.setNotebookName(notebookName);
        }
        if (notebookId_old==null && notebookId_new==null){
        }else if (notebookId_old!=null && notebookId_new!=null){
            if (!notebookId_old.equals(notebookId_new)){
                int i = notebookMapper.updateNoteNum(notebookId_old, notebookId_new);
            }
        }else {
            int i = notebookMapper.updateNoteNum(notebookId_old, notebookId_new);
        }
        return noteMapper.updateNote(note);
    }

    /**
     * 批量删除笔记
     * 
     * @param ids 需要删除的笔记主键
     * @return 结果
     */
    @Override
    public int deleteNoteByIds(String[] ids)
    {
        for (String id:ids){
            Note note = noteMapper.selectNoteById(id);
            Long notebookId = note.getNotebookId();
            if (notebookId!=null){
                notebookMapper.updateNoteNum(notebookId,null);
            }
        }
        return noteMapper.deleteNoteByIds(ids);
    }
    @Override
    public int deleteNoteByIds_status(String[] ids)
    {
        String updateDate = DateUtils.getTime();
        return noteMapper.deleteNoteByIds_status(ids,updateDate);
    }
    @Override
    public int updateNoteByIds_status(String[] ids)
    {
        String updateDate = DateUtils.getTime();
        return noteMapper.updateNoteByIds_status(ids,updateDate);
    }

    /**
     * 删除笔记信息
     * 
     * @param id 笔记主键
     * @return 结果
     */
    @Override
    public int deleteNoteById(String id)
    {
        return noteMapper.deleteNoteById(id);
    }
}
